﻿using System;
using System.Linq;
using System.Windows;
using System.Threading;
using System.Collections.ObjectModel;

// Toolkit namespace
using GUIEXAM13;
using SimpleMvvmToolkit;

namespace GUIEXAM13
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvmprop</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// </summary>
    public class CreateEventViewModel : ViewModelDetailBase<CreateEventViewModel, EventDomain>, IViewModel
    {
        public Clothing ClothingModel;
        public Events EventsModel;

        private readonly IViewModelMediator _viewModelMediator;

        public CreateEventViewModel(Clothing clothingModel, Events eventsModel, IViewModelMediator viewModelMediator)
        {
            ClothingModel = clothingModel;
            EventsModel = eventsModel;
            _viewModelMediator = viewModelMediator;
            _viewModelMediator.Register(this);
            AddedClothing = CurrentEvent.Clothing;
        }

        private EventDomain _currentEvent;
        public EventDomain CurrentEvent
        {
            get { return _currentEvent = _currentEvent ?? new EventDomain(); }
            set
            {
                _currentEvent = value;
                AddedClothing = _currentEvent.Clothing;
                NotifyPropertyChanged(m => m.CurrentEvent);
            }
        }
        
        public ObservableCollection<ClothingDomain> AllClothing
        {
            get { return ClothingModel.AllClothing; }
            set
            {
                ClothingModel.AllClothing = value;
                NotifyPropertyChanged(m => m.ClothingModel.AllClothing);
            }
        }

        private ClothingDomain _selectedClothing;
        public ClothingDomain SelectedClothing
        {
            get { return _selectedClothing; }
            set
            {
                _selectedClothing = value;
                NotifyPropertyChanged(m => m.SelectedClothing);
            }
        }

        public void DeleteSelectedClothing()
        {
            AllClothing.Remove(SelectedClothing);
            try { SelectedClothing = AllClothing[0]; }
            catch (Exception) {}
        }

        private ObservableCollection<ClothingDomain> _addedClothing;
        public ObservableCollection<ClothingDomain> AddedClothing
        {
            get { return _addedClothing = _addedClothing ?? new ObservableCollection<ClothingDomain>(); }
            set
            {
                _addedClothing = value;
                NotifyPropertyChanged(m => m.AddedClothing);
            }
        }

        private ClothingDomain _selectedAddedClothing;
        public ClothingDomain SelectedAddedClothing
        {
            get { return _selectedAddedClothing; }
            set
            {
                _selectedAddedClothing = value;
                NotifyPropertyChanged(m => m.SelectedAddedClothing);
            }
        }

        public void RemoveSelectedClothing()
        {
            AddedClothing.Remove(SelectedAddedClothing);
            try { SelectedAddedClothing = AddedClothing[0]; }
            catch (Exception){}
        }

        public void AddSelectedClothing()
        {
            if(SelectedClothing != null)
                AddedClothing.Add(SelectedClothing);
        }

        public void SaveChanges()
        {
            ClothingModel.SaveAllClothing();

            CurrentEvent.Clothing = AddedClothing;
            
            try
            {
                EventsModel.AllEvents.Remove(CurrentEvent);
            } catch (Exception){}
            
            EventsModel.SaveAllEvents();

            ResetViewModel();
        }

        public void ResetViewModel()
        {
            CurrentEvent = new EventDomain();
            AddedClothing = new ObservableCollection<ClothingDomain>();
        }

        public void DiscardChanges()
        {
            EventsModel.AllEvents.Remove(CurrentEvent);
        }

        //Mediator stuff
        public string GetName()
        {
            return "CreateEventViewModel";
        }

        public void Send(string receiver, ViewModelMessage message)
        {
            throw new NotImplementedException();
        }

        public void Receive(string from, ViewModelMessage message)
        {
            switch (message.Type)
            {
                case ViewModelMessageType.Event:
                    CurrentEvent = message.Message as EventDomain;
                    break;

                default:
                    Console.Write("Unknown Message Received");
                    break;
            }
        }
    }
}